// pages/my/vip/vip_myCard/index.js
const getMyVipCard = require("../../../controllers/ControllerVip").controller.getMyVipCard;
const getVipDetail = require("../../../controllers/ControllerVip").controller.getVipDetail;
const deleteMyVip = require("../../../controllers/ControllerVip").controller.deleteMyVip;

const Controllers = require("../../../controllers/Controller").controller

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDel: false,
    globalStype: '',
    cardList: [],
    lastPage: false,
    type: "",
    pageNumber: 1,
    src1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756367236956160.png",
    src2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756346697449472.png",
    src3: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211203/916340952883265536.png",
    vip_bg: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211126/913795093343764480.png",
  },

  //修改已读
  readNum() {
    let value = {}
    value.shopId = wx.getStorageSync('shopId')
    value.name = "shopVip"
    value.num = this.data.num
    Controllers.readNum(value).then(res => {

    })
  },

  //跳转赠送详情
  toGiveHistory(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/my/vip/vip_myCard/giveHistory?id=' + id,
    })
  },

  //跳转开卡页面
  toOpenCard() {
    wx.navigateTo({
      url: '/pages/my/vip/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      globalStype: app.globalData.globalStype,
    })
    if (options && options.type) {
      this.setData({
        type: options.type
      })
    }
    if (options && options.num) {
      this.setData({
        num: options.num
      })
      this.readNum()
    }
  },

  onShow: function () {
    this.getMyCardList(true)
  },

  onReachBottom() {
    if (this.data.lastPage) return
    this.getMyCardList()
  },

  // 进入充值页面
  jumpVip_renew() {
    wx.navigateTo({
      url: '/pages/my/vip/vip_renew/index',
    });
  },
  //进入使用说明页
  jumpVip_explain(e) {
    let index = e.currentTarget.dataset.index;
    app.globalData.vipDesc = this.data.cardList[index].desc;
    wx.navigateTo({
      url: '/pages/my/vip/vip_explain/index',
    })
  },
  //获取我的会员卡列表
  getMyCardList(isFirst = false) {
    let shopId = app.globalData.shopId;
    let pageNumber = this.data.pageNumber + 1;
    let cardList = this.data.cardList
    if (isFirst) {
      pageNumber = 1;
      cardList = []
    }
    getMyVipCard({
      shopId,
      pageNumber,
      pageSize: 4,
      type: this.data.type
    }).then(res => {
      cardList = cardList.concat(res.data.list)
      this.setData({
        cardList,
        pageNumber,
        lastPage: res.data.lastPage,
      })
    })
  },

  handleShowDelClick() {
    let showDel = this.data.showDel
    showDel = !showDel
    this.setData({
      showDel
    })
  },
  //删除VIP卡
  deleteMyVip(e) {
    const that = this
    let accountVipId = e.currentTarget.dataset.accountvipid;
    wx.showModal({
      title: "是否删除该会员卡？",
      success(res) {
        if (res.confirm) {
          deleteMyVip({
            accountVipId
          }).then(res => {
            wx.showToast({
              title: '删除成功',
              icon:"none",
              duration:2000
            })
            that.getMyCardList(true)
          })
        }
      }
    })

  },

  //跳转会员卡详情页
  jumpToCardDetail(e) {
    let id = e.currentTarget.dataset.id;
    // let url="/pages/my/vip/vipCard_detail/index?id="+id;
    let url = "/pages/my/vip/my/VIPCardInfo?id=" + id;
    wx.navigateTo({
      url: url
    })
  },

  //获取会员卡包含的数据并判断是否为空
  // getVipCardDetail(id,url){
  //   let shopId=app.globalData.shopId;
  //   getVipDetail({shopId:shopId,accountVipId:id}).then(res=>{
  //     console.log("####",res.data);
  //     // if(res.data.vipGoodsList!=false){

  //     // }

  //     wx.navigateTo({
  //       url: url
  //     })
  //   })
  // }
})